package com.example.new_music;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.new_music.adapter.welcomeAdapter;
import com.example.new_music.fragment.one;
import com.example.new_music.fragment.three;
import com.example.new_music.fragment.two;

import java.util.ArrayList;

public class Main2Activity extends AppCompatActivity {
    private ViewPager vp;
    private RadioButton button1;
    private RadioButton button2;
    private RadioButton button3;
    private RadioGroup rg;
    private ArrayList<Fragment> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
    }

    private void initView() {
        vp = (ViewPager) findViewById(R.id.vp);
        button1 = (RadioButton) findViewById(R.id.button1);
        button2 = (RadioButton) findViewById(R.id.button2);
        button3 = (RadioButton) findViewById(R.id.button3);
        rg = (RadioGroup) findViewById(R.id.rg);
        one one = new one();
        two two = new two();
        three three = new three();
        list.add(one);
        list.add(two);
        list.add(three);
        welcomeAdapter welcomeAapter = new welcomeAdapter(getSupportFragmentManager(), list);
        vp.setAdapter(welcomeAapter);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.button1:
                        vp.setCurrentItem(0, true);
                        break;
                    case R.id.button2:
                        vp.setCurrentItem(1, true);
                        break;
                    case R.id.button3:
                        vp.setCurrentItem(2, true);
                        break;
                }
            }
        });
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if(i==0){
                    button1.setChecked(true);
                }else if(i==1){
                    button2.setChecked(true);
                }else if(i==2){
                    button3.setChecked(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }
}
